<?php
/**
 * 前端用户账户信息服务
 * Created by PhpStorm.
 * User: 
 * Date: 
 * Time: 
 */

namespace app\services\controller;

use think\captcha\Captcha;
use think\Config;
use think\Db;
use think\Request;
use think\Response;
use weixin\weixin;
use app\model\UserBase;
use ethink\mvc\BaseController;
use ethink\acl\Auth;
use think\Loader;

class Question extends BaseController
{
    private $userId;
    private $roleId;
    public function __construct(Request $request = null)
    {
        $this->auth = new Auth(Config::get('api')['auth']);
        parent::__construct($request);
    }
 
    /**
     * [checkRole description]
     * @return [type] [description]
     */
    private function checkRole(){
      //1校验登陆状态
      $user = $this->checkUserStatus();  
      trace("admin,checkRole:".json_encode($user));
      if($user['status']){
          return $user['status'];
      }
      $this->userId = $user['session']['userId'];
      $this->roleId = $user['session']['roleId'];
      // var_dump($user);exit;
      //此控制器的所有权限都要求是管理员
      $import = $this->checkCaseRole($this->roleId,['admin']);
      trace("question,checkCaseRole,ret:$import");
      return $import;
    }



     /**
     * [质疑类型 列表]
     * @return [type] [description]
     */
    public function questionTypeList(){
        $ret = $this->checkRole();
        if($ret){
          return $this->responseJson(null, $ret);          
        }

        if($this->request->isGet()){
            $reqData = $this->request->get();
            trace("case-questionTypeList,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }            
           
            $userId = $this->userId;
            $reqData['roleId'] = $this->roleId;
            //2查询数据
            $Model = new \app\services\model\QuestionType();
            $reqData['pageSize'] = Config::get('api')['list']['page_size'];
            $list = $Model->typeList($userId,$reqData);
            // var_dump($list);exit;
            if($list['status'])
                return $this->responseJson(null, $list['status']);
                // return $this->responseJson(null, $list['status']);
            $retdata['data'] =$list['info']->toArray();
            return $this->responseJson($retdata, 0);                      
        }      
    }   

     /**
     * [所有质疑类型]
     * @return [type] [description]
     */
    public function questionTypeAll(){
        
        if($this->request->isGet()){
            $reqData = $this->request->get();
            trace("case-questionTypeAll,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }            
           
            $userId = $this->userId;
            $reqData['roleId'] = $this->roleId;
            //2查询数据
            $Model = new \app\services\model\QuestionType();
            $list = $Model->typeListAll($userId,$reqData);
            $ret['data'] = $list;
            return $this->responseJson($ret, 0);                      
        }      
    }   


     /**
     * [addtype 添加编辑质疑类型]
     * @return [type] [description]
     */
    public function addType(){
        $ret = $this->checkRole();
        if($ret){
          return $this->responseJson(null, $ret);          
        }

        if($this->request->isPost()){     
            $reqData = $this->getJsonData(['id', 'type' , 'instruction']);
            trace("addtype,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }            
            
            $userId = $this->userId;
            $reqData['roleId'] =  $this->roleId;
            //2查询数据
            $Model = new \app\services\model\QuestionType();
            $retData = $Model->addType($userId,$reqData);
            return $this->responseJson(null, $retData); 

        }      
    } /**
     * [delType 删除 质疑类型]
     * @return [type] [description]
     */
    public function delType(){

        $ret = $this->checkRole();
        if($ret){
          return $this->responseJson(null, $ret);          
        }

        if($this->request->isPost()){     
            $reqData = $this->getJsonData(['id']);
            trace("delType,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }            
           
            $userId = $this->userId;
            $reqData['roleId'] = $this->roleId;
            //2查询数据
            $Model = new \app\services\model\QuestionType();
            $retData = $Model->delType($userId,$reqData['id']);
            return $this->responseJson(null, $retData); 

        }      
    }

}